<template>
	<view class="example-details">
		<view class="h2" v-if="exampleData.studentId&&student_name">
			<text>{{student_name}}</text>创建于{{utils.dateFormat(exampleData.addTime.split(' ')[0])}}
		</view>
		<view class="xd-box-shadow example-info">
            <view class="info flex">
                <view class="img">
                    <image :src="api.xiaodeObsUrl+exampleData.pictureUrl" mode="aspectFill"></image>
                </view>
                <view class="name-class flex">
                    <view class="name">
                        {{exampleData.name}}<br>
                        <text v-if="exampleData.characterType">{{exampleData.characterType==1?'现实人物':'虚拟人物'}}</text>
                    </view>
                    <view v-if="exampleData.count>0" style="color: #7F7F7F;">
                        {{exampleData.count}}位同学添加为重要他人
                    </view>
                    
                </view>
            </view>
            <view class="reason" v-if="exampleData.reason&&JSON.parse(exampleData.reason).length>0">
                <view class="title">
                    <text>选择理由</text>
                </view>
                <view class="list">
                    <view v-for="(item, i) in JSON.parse(exampleData.reason)" :key="i">{{i+1}}.{{item}}{{i==2 ? '':'；'}}</view>
                </view>
            </view>
            <view class="introduce">
                <view class="title">基本特征</view>
                {{exampleData.brief}}
            </view>
            <view class="bottom-btn">
                <view class="box" v-if="exampleData.state==1&&showBottomBtn">
                    <text class="refused show-list-btn" @click="setExamine(2)">通过</text>
                    <text class="through show-list-btn" @click="inputDialogToggle(2)">拒绝</text>
                </view>
                <view class="box" v-else>
                    <text v-if="exampleData.state==3 || exampleData.state==5">已拒绝</text>
                    <text v-if="exampleData.state==2">待组长审核</text>
                    <!-- <text v-if="exampleData.state==4" class="refused show-list-btn" @click="inputDialogToggle(1)">申请删除</text> -->
                </view>
            </view>
        </view>
        <view class="task-list xd-box-shadow">
            <view class="title">
                <text>任务详情</text>
                <text class="time">任务期限：{{deadline}}</text>
            </view>
            <view class="tasks">
                <view class="task-item" v-for="task in exampleTasks" :key="task.id">
                    <text class="cont">{{task.content}}</text>
                    <view class="btn" v-if="task.state==1">
                        <text class="show-list-btn" @click="setTask(task, 2)">通过</text>
                        <text class="through show-list-btn" @click="inputDialogToggle(3, task)">拒绝</text>
                    </view>
                    <view class="btn" v-else>
                        {{task.state==2?'待组长审核':task.state==4?'审核通过':'已拒绝'}}
                    </view>
                </view>
            </view>
        </view>
        <view class="change-btn" v-if="cur_exam_index!=-1">
            <text class="prev" :style="{opacity: cur_exam_index==0?.5:1}" @click="onPrev()">上一个</text>
            <text class="next" :style="{opacity: cur_exam_index==(examineList.length-1)?.5:1}" @click="onNext()">下一个</text>
        </view>
        <view>
            <uni-popup ref="inputDialog" type="dialog">
                <uni-popup-dialog ref="inputClose"  mode="input" :title="input_popup==1?'申请删除':'审核拒绝'" value=""
                    :placeholder="input_popup==1?'请输入申请删除原因':'请输入拒绝的理由'" @confirm="dialogInputConfirm"></uni-popup-dialog>
            </uni-popup>
        </view>
	</view>
</template>

<script>
	import uniPopup from "@/components/uni-popup/uni-popup"
	import uniPopupDialog from "@/components/uni-popup-dialog/uni-popup-dialog"
	export default {
		components: {
			uniPopupDialog,
			uniPopup
		},
		data(){
			return{
				options: {},
                student_name: '',
				cur_exam_index: -1,
				exam_stu_id: '',
				exampleData: {},
				exampleTasks: [],
				currentTask: {},
				deadline: '',
				showBottomBtn: true,
				del_value: '',
				userInfo: {},
                examineList: [],
				input_popup: 0
			}
		},
		onLoad(options) {
            this.examineList = uni.getStorageSync('msgExampleList') || []
			this.exam_stu_id = options.stuId;
            this.cur_exam_index = parseInt(options.index)
            this.student_name = this.examineList[options.index]?this.examineList[options.index].name:''
			if(options.stuId){
				this.getExampleDetails()
			}
		},
		methods: {
			inputDialogToggle(type, task){
				this.input_popup = type;
				if(type == 3){
					this.currentTask = task;
				}
				this.$refs.inputDialog.open()
			},
			dialogInputConfirm(val) {
				// 关闭窗口后，恢复默认内容
				if(this.input_popup==1){
					this.delExample(val)
				}else if(this.input_popup==2){
					this.setExamine(3, val)
				}else if(this.input_popup==3){
					this.setTask(this.currentTask, 3, val)
				}
				uni.showLoading({mask: true})
				this.$refs.inputDialog.close()
			},
			getExampleDetails(){
				this.showBottomBtn = true;
				this.userInfo = getApp().globalData.userInfo;
				this.api.get('tExample/exampleDetailPage', {
					studentId: this.exam_stu_id
				}).then(res=>{
					this.exampleData = res.data.exampleDetails;
					let tasks = this.exampleData.tasks;
					this.exampleTasks = tasks;
					let end_time = tasks[0] ? tasks[0].deadline : '';
					if(end_time){
						this.deadline = end_time.split(' ')[0];
					}
				})
			},
			setExamine(state, val){
				this.api.post('tExample/examineExample', {
					exampleId: this.exampleData.id,
					state,
					examineReason: val
				}).then(res=>{
					uni.hideLoading()
					if(res.data.state == 'ok'){
						this.handleZytr(parseInt(this.exampleData.id), 'shbydss')
                        this.exampleData.state = state;
						this.showBottomBtn = false;
					}
				})
			},
			delExample(val){
				this.api.post('tExample/delExamplApply', {
					exId: this.exampleData.id,
					reason: val
				}).then(res=>{
					if(res.data.state == 'ok'){
						uni.hideLoading()
						uni.showToast({
							title: '已申请删除',
							duration: 2000,
							icon: 'none',
							mask: true,
							position: 'bottom'
						})
					}
				})
			},
			setTask(tsItem,state, val){
				uni.showLoading({mask: true})
				this.api.post('tExample/examineTask', {
					setId: tsItem.id,
					state,
					reason: state==3?val:''
				}).then(res=>{
					uni.hideLoading()
					if(res.data.state == 'ok'){
						this.handleZytr(parseInt(tsItem.id), 'shbyrwdss')
						this.$set(tsItem, 'state', state);
					}
				})
			},
			onPrev(){
				if(this.cur_exam_index==0) return;
				this.cur_exam_index -= 1;
				this.changeExample()
			},
			onNext(){
				if(this.cur_exam_index==(this.examineList.length-1)) return;
				this.cur_exam_index += 1;
				this.changeExample()
			},
			changeExample(){

				let item = this.examineList[this.cur_exam_index]
				this.exam_stu_id = item.id
                this.student_name = item.name
				this.getExampleDetails();
			},
			handleZytr(id, type){
				let bdxs_shbyrwdss = getApp().globalData.bdxs_shbyrwdss || {},
				shbyrwdss =  bdxs_shbyrwdss[type] || [];
				if(shbyrwdss.length>0){
					let index = shbyrwdss.indexOf(parseInt(id))
					shbyrwdss.splice(index, 1)
					getApp().globalData.bdxs_shbyrwdss = bdxs_shbyrwdss
				}
			}
		}
	}
</script>

<style lang="less">
	.example-details{
		padding: 32rpx;
		.h2{
			position: relative;
			line-height: 48rpx;
			margin-bottom: 24rpx;
			color: #aaa;
			text{
				color: #02A7F0;
			}
		}
		.example-info{
			padding: 32rpx;
			.info{
				
			}
			.img{
				image{
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
					background: #f5f5f5;
				}
			}
			.name-class{
				width: calc(100% - 224rpx);
				margin-left: 24rpx;
				flex-direction: column;
				justify-content: space-between;
			}
			.name{
				font-size: 36rpx;
				color: #000;
				line-height: 44rpx;
				text{
					color: #555;
				}
			}
			.reason{
				margin-top: 32rpx;
				.title{
					font-size: 0;
					text{
						font-size: 32rpx;
						color: #333;
						vertical-align: text-bottom;
					}
				}
				.list{
                    margin-top: 20rpx;
                    view{
                        margin-bottom: 8rpx;
                    }
				}
			}
			.introduce{
				margin-top: 40rpx;
				line-height: 44rpx;
				.title{
					font-size: 32rpx;
					color: #333;
				}
			}
			.bottom-btn{
				margin-top: 40rpx;
				text-align: right;
				.box{
				}
				.refused{
					padding: 0 16rpx;
				}
				
				.through{
					margin-left: 32rpx;
					background: #d7d7d7;
					color: #7f7f7f;
				}
			}
		}
		.flex{
			display: flex;
		}
		.task-list{
			margin-top: 32rpx;
			padding: 32rpx;
			.title{
				display: flex;
				justify-content: space-between;
				align-items: flex-end;
				padding-bottom: 8rpx;
				border-bottom: 2rpx solid #f2f2f2;
				font-size: 32rpx;
				.time{
					font-size: 28rpx;
				}
			}
			.tasks{
				margin-top: 24rpx;
			}
			.task-item{
				padding: 8rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.cont{
					width: 460rpx;
				}
				.btn{
					width: 136rpx;
					white-space: nowrap;
					text-align: center;
					text{
						height: 38rpx;
						line-height: 38rpx;
						padding: 0 8rpx;
					}
				}
				.through{
					margin-left: 8rpx;
					background: #d7d7d7;
					color: #7f7f7f;
				}
			}
		}
		.change-btn{
			margin-top: 32rpx;
			text{
				display: inline-block;
				width: 50%;
				height: 56rpx;
				line-height: 56rpx;
				text-align: center;
			}
			.prev{
				background-color: #81d3f8;
				color: #555;
			}
			.next{
				background-color: #55acee;
				color: #f2f2f2;
			}
		}
	}
</style>
